package com.example.yang.yixin.Activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.yang.yixin.Adapter.MusicGridAdapter;

import com.example.yang.yixin.R;
import com.example.yang.yixin.Utils.Class_info;
import com.example.yang.yixin.Utils.ScreenLock;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity{
private List<Class_info> classnameList=new ArrayList<>();
private RecyclerView mRecyclerView;
private MusicGridAdapter madpater;
private SmartRefreshLayout smartRefreshLayout;
    private ScreenLock screenLock;
    private SharedPreferences pref;
    public SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView=findViewById(R.id.iv_recycler);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        smartRefreshLayout=findViewById(R.id.smart_refresh);
        initView();
        getServerData();
        getUserinfo();
        screenLock=new ScreenLock(this);
        screenLock.activeAdmin();
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(2000);
                classnameList.clear();
                getServerData();
            }
        });

    }

    private void getUserinfo() {
        String jsonurl = "http://47.106.141.161/MyFirstAndroidApp/IdentityServlet?username="+getIntent().getStringExtra("data");
        RequestQueue mQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonurl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {

                    String identity;
                    identity=new String(response.getString("identity"));

                    pref = getSharedPreferences("user", MODE_PRIVATE);
                    editor=pref.edit();
                    editor.putString("identity",identity);
                    editor.apply();

                } catch (JSONException e) {



                }

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("TAG错误", volleyError.getMessage(), volleyError);
                    }
                }) {
            protected Response<JSONObject> paraeNetworkResponse(NetworkResponse response) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(response.data, "utf-8"));
                    return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
                }catch (JSONException je) {
                    return Response.error(new ParseError(je));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                }

            }
        };


        mQueue.add(jsonObjectRequest);
    }


    private void initView() {
        initNarvBar(false,"友课",true);

    }
    public  void getServerData() {
        String jsonurl = "http://47.106.141.161/MyFirstAndroidApp/CourseSelect?username="+getIntent().getStringExtra("data");
        RequestQueue mQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonurl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {

                    String classname=-1+"";
                    String coursename;
                    String bkpassword;
                    String username;
                    int size=Integer.parseInt(response.getString("size")) ;

                        for (int i = 0; i<size; i++) {
                        /*
                        这个地方要修改
                         */
                            classname = new String(response.getString("classname"+ i));
                            coursename = new String(response.getString("coursename" + i));
                            bkpassword = new String(response.getString("bkpassword"+i));
                            username = getIntent().getStringExtra("data");
                            Class_info a = new Class_info(classname, coursename, bkpassword, username);
                            classnameList.add(a);
                        }
                      Log.d("测试",classname+" ");




                } catch (JSONException e) {
                    String classname;
                    String coursename;
                    String bkpassword;
                    String username;
                    classname="请完善个人信息后再加入班课！";
                    coursename="你尚未加入课程任何课程";
                    bkpassword="";
                    username = getIntent().getStringExtra("data");
                    Class_info a = new Class_info(classname, coursename, bkpassword, username);
                    classnameList.add(a);

                }
                madpater=new MusicGridAdapter(classnameList);

                mRecyclerView.setAdapter(madpater);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("TAG错误", volleyError.getMessage(), volleyError);
                    }
                }) {
            protected Response<JSONObject> paraeNetworkResponse(NetworkResponse response) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(response.data, "utf-8"));
                    return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
                }catch (JSONException je) {
                    return Response.error(new ParseError(je));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                }

            }
        };


        mQueue.add(jsonObjectRequest);

    }

}


